package com.teach.javafxclient.controller.base;

import com.teach.javafxclient.Globalvar;
import com.teach.javafxclient.MainApplication;
import com.teach.javafxclient.util.HttpUtil;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.layout.VBox;
import kong.unirest.json.JSONArray;
import kong.unirest.json.JSONObject;

import java.io.IOException;

/**
 * @author Liang Jianlin
 * @date 2024-05-30 16:54
 */
public class HonorPageController {
    @FXML
    private VBox honorContainer;
    public void set(Long studentId) throws IOException {
        JSONArray res = HttpUtil.get(Globalvar.baseUrl+"/api/users/"+studentId+"/honors")
                .header("Accept", "application/json")
                .asJson()
                .getBody()
                .getArray();
        for (int i = 0; i < res.length(); i++) {
            JSONObject jo = res.getJSONObject(i);
            FXMLLoader fxmlLoader = new FXMLLoader(MainApplication.class.getResource("base/honor-item.fxml"));
            Parent root = fxmlLoader.load();
            HonorItemController HIC = fxmlLoader.getController();
            HIC.name.setText(jo.getString("name"));
            HIC.type.setText(jo.getString("type"));
            HIC.description.setText(jo.getString("description"));
            HIC.time.setText(jo.getString("awardTime"));
            honorContainer.getChildren().add(root);
        }
    }
}